//
//  ZCMediaView.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/28.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCMediaView.h"
#import "ZCEssenceModel.h"
#import "UIImage+GIF.h"
#import "DALabeledCircularProgressView.h"
#import "ZCShowBigPictureViewController.h"
#import <DOUAudioStreamer.h>



@interface ZCMediaView ()

@property (weak, nonatomic) DALabeledCircularProgressView *progressView;

@property (weak, nonatomic) UIImageView *pictureView;
@property (weak, nonatomic) UIImageView *markImage;
@property (weak, nonatomic) UIButton *seeBigPicBtn;

@property (weak, nonatomic) UIImageView *videoView;//影音播放视图
@property (weak, nonatomic) UIButton *videoPlayBtn;//视频播放按钮
@property (weak, nonatomic) UIButton *voicePlayBtn;//声音播放按钮
@property (weak, nonatomic) UILabel *playCountLab;//播放次数
@property (weak, nonatomic) UILabel *playLengthLab;//播放时长

@property (weak, nonatomic) UIView *coverView;

@property (weak, nonatomic) MASConstraint *pictureViewHeight;
@property (weak, nonatomic) MASConstraint *videoViewHeight;
@property (weak, nonatomic) MASConstraint *videoViewTop;
@property (weak, nonatomic) MASConstraint *selfViewHeight;




@end

@implementation ZCMediaView


-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
    
        [self setupUI];

        
    }
    
    return self;
}
/**
 *  MARK:页面布局
 */
-(void)setupUI{
    
    /** MARK:图片视图 */
    UIImageView *pictureView = [[UIImageView alloc]init];
    self.pictureView = pictureView;
    pictureView.backgroundColor = [UIColor grayColor];
    pictureView.contentMode = UIViewContentModeScaleAspectFill;
    pictureView.clipsToBounds = YES;
    pictureView.userInteractionEnabled = YES;
    [self addSubview:pictureView];
    
    UITapGestureRecognizer *tepGesture  = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pictureViewTepGestureAciton)];
    [pictureView addGestureRecognizer:tepGesture];
    
    
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self);
        make.leading.trailing.equalTo(self);
        self.pictureViewHeight = make.height.mas_equalTo(0);
    }];
    
    //GIF标记图
    UIImageView *markImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common-gif"]];
    self.markImage = markImage;
    [self.pictureView addSubview:markImage];
    [pictureView addSubview:markImage];
    
    [markImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.equalTo(self.pictureView);
        make.width.height.mas_equalTo(31);
    }];
    
    //点击查看大图
    UIButton *seeBigPicBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.seeBigPicBtn = seeBigPicBtn;
    seeBigPicBtn.adjustsImageWhenHighlighted = NO;
    [seeBigPicBtn setTitle:@" 点击查看大图" forState:UIControlStateNormal];
    [seeBigPicBtn setImage:[UIImage imageNamed:@"see-big-picture"] forState:UIControlStateNormal];
    [seeBigPicBtn setBackgroundImage:[UIImage imageNamed:@"see-big-picture-background"] forState:UIControlStateNormal];
    [seeBigPicBtn addTarget:self action:@selector(pictureViewTepGestureAciton) forControlEvents:UIControlEventTouchUpInside];
    [self.pictureView addSubview:seeBigPicBtn];
    [pictureView addSubview:seeBigPicBtn];
    
    
    
    
    [seeBigPicBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.leading.trailing.equalTo(self.pictureView);
        make.height.mas_equalTo(35);
    }];
    
    
    
    /** MARK:影音视图 */
    UIImageView *videoView = [[UIImageView alloc]init];
    videoView.userInteractionEnabled = YES;
    videoView.backgroundColor = [UIColor grayColor];
    self.videoView = videoView;
    [self addSubview:videoView];
    
    [videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.videoViewTop = make.top.equalTo(pictureView.mas_bottom);
        make.leading.trailing.equalTo(self);
        self.videoViewHeight = make.height.mas_equalTo(0);
    }];
    
    //播放次数
    UILabel *playCountLab = [UILabel labelWithText:@"播放次数" textColor:[UIColor whiteColor] fontSize:14 textAlignment:NSTextAlignmentCenter];
    self.playCountLab = playCountLab;
    playCountLab.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [videoView addSubview:playCountLab];
    
    [playCountLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.bottom.equalTo(videoView);
    }];
    
    //播放时长
    UILabel *playLengthLab = [UILabel labelWithText:@"播放时长" textColor:[UIColor whiteColor] fontSize:14 textAlignment:NSTextAlignmentCenter];
    self.playLengthLab = playLengthLab;
    playLengthLab.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [videoView addSubview:playLengthLab];
    
    [playLengthLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.bottom.equalTo(videoView);
    }];
    
    //视频播放按钮
    UIButton *videoPlayBtn = [UIButton buttonWithBackgroundImage:@"video-play" selsectedImage:@"playButtonPause"];
    self.videoPlayBtn = videoPlayBtn;
    [videoView addSubview:videoPlayBtn];
    
    [videoPlayBtn addTarget:self action:@selector(videoPlayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [videoPlayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(videoView);
    }];
    
    //声音播放按钮
    UIButton *voicePlayBtn = [UIButton buttonWithBackgroundImage:@"playButton" selsectedImage:@"playButtonClick"];
    self.voicePlayBtn = voicePlayBtn;
    [voicePlayBtn setImage:[UIImage imageNamed:@"playButtonPlay"] forState:UIControlStateNormal];
    [voicePlayBtn setImage:[UIImage imageNamed:@"playButtonPause"] forState:UIControlStateSelected];
    [videoView addSubview:voicePlayBtn];
    
    [voicePlayBtn addTarget:self action:@selector(voicePlayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [voicePlayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(videoView);
        make.bottom.equalTo(videoView);
    }];
    
    
    
    //cell底部约束
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        self.selfViewHeight = make.bottom.equalTo(videoView.mas_bottom);
    }];
    
}

-(void)setModel:(ZCEssenceModel *)model{
    
    _model = model;
    
    CGFloat newH = (model.height.floatValue / model.width.floatValue) * ([UIScreen mainScreen].bounds.size.width-20);

    if (![model.voicetime isEqualToString:@"0"] || ![model.videotime isEqualToString:@"0"]) {
        self.playLengthLab.alpha = 1;
        self.playCountLab.alpha = 1;
        if (![model.voicetime isEqualToString:@"0"]) {
            self.voicePlayBtn.alpha = 1;
            self.videoPlayBtn.alpha = 0;
            self.playLengthLab.text = model.voiceTIME;
            [self videoViewDownloadDataWithModel:model newHeight:newH];
        }else{
            self.voicePlayBtn.alpha = 0;
            self.videoPlayBtn.alpha = 1;
            self.playLengthLab.text = model.videoTIME;
        
            [self videoViewDownloadDataWithModel:model newHeight:newH];
        }
        
    }else{
        self.playCountLab.alpha = 0;
        self.playLengthLab.alpha = 0;
        if (model.is_gif.intValue) {
            
            self.markImage.alpha = 1;
            self.seeBigPicBtn.alpha = 0;
            [self pictureViewDownloadDataWithModel:model newHeight:newH];
            
        }else if(model.height.intValue > 2000){
            
            self.markImage.alpha = 0;
            self.seeBigPicBtn.alpha = 1;
            
            [self pictureViewDownloadDataWithModel:model newHeight:200];
            
        }else if (model.type.integerValue == Picture){
            
            self.markImage.alpha = 0;
            self.seeBigPicBtn.alpha = 0;
            
//            ZCLog(@"%f",newH);
//            ZCLog(@"%@",model.text);
            
            [self pictureViewDownloadDataWithModel:model newHeight:newH];
        }
    }
}

/**
 *  图片视图下载数据
 *
 *  @param model     数据模型
 *  @param newHeight 视图的新高度
 */
-(void)pictureViewDownloadDataWithModel:(ZCEssenceModel*)model newHeight:(CGFloat)newHeight{
    
    
    self.videoView.hidden = YES;
    self.pictureView.hidden = NO;
    [self.pictureViewHeight uninstall];
    [self.selfViewHeight uninstall];
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.pictureViewHeight = make.height.mas_equalTo(newHeight);
    }];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        self.selfViewHeight = make.bottom.equalTo(self.pictureView.mas_bottom);
    }];
    
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    
    
    
    [self showProgressWithDisplayView:self.pictureView];
    
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:model.image2] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        CGFloat progress = 1.0 *receivedSize/expectedSize;
        
        [self.progressView setProgress:progress animated:YES];
        
        self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.0f",progress*100];
        
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (error == nil) {
            ZCLog(@"完成");
            [self.coverView removeFromSuperview];
        }
        
    }];

}
/**
 *  影音视图下载数据
 *
 *  @param model     数据模型
 *  @param newHeight 视图新高度
 */
-(void)videoViewDownloadDataWithModel:(ZCEssenceModel*)model newHeight:(CGFloat)newHeight{
    
    self.pictureView.hidden = YES;
    self.videoView.hidden = NO;
    self.playCountLab.text = [NSString stringWithFormat:@" %@次播放 ",model.playcount];
    
    [self.videoViewHeight uninstall];
    [self.videoViewTop uninstall];
    [self.selfViewHeight uninstall];
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        self.videoViewHeight = make.height.mas_equalTo(newHeight);
        self.videoViewTop = make.top.equalTo(self);
    }];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        self.selfViewHeight = make.bottom.equalTo(self.videoView.mas_bottom);
    }];
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
    [self layoutIfNeeded];
    
    [self.videoView sd_setImageWithURL:[NSURL URLWithString:model.image2] placeholderImage:nil options:SDWebImageProgressiveDownload progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
        CGFloat progress = (CGFloat)receivedSize/(CGFloat)expectedSize;
        ZCLog(@"%02f %% 动态图",progress*100);
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (error == nil) {
            
        }
        
    }];
}


/**
 *  显示下载进度
 *
 *  @param displayView 需要显示的视图
 *
 *  @return 显示下载进度的视图
 */
-(UIView *)showProgressWithDisplayView:(UIView*)displayView{
    
    //设置加载视图
    UIView *coverView = [[UIView alloc]init];
    self.coverView = coverView;
    coverView.backgroundColor = ZCRGBColor(233, 233, 233);
    [displayView addSubview:coverView];
    
    
    UIButton *waitImage= [UIButton buttonWithBackgroundImage:@"imageBackground"];
    [self.coverView addSubview:waitImage];
    
    DALabeledCircularProgressView *progressView = [[DALabeledCircularProgressView alloc]init];
    self.progressView = progressView;
    progressView.progressLabel.textColor = [UIColor whiteColor];
    progressView.progressLabel.font = [UIFont systemFontOfSize:15];
    progressView.backgroundColor = [UIColor clearColor];
    
    [self.coverView addSubview:progressView];
    
    
    [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.leading.trailing.equalTo(displayView);
    }];
    [waitImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(coverView.mas_top).offset(20);
        make.centerX.equalTo(coverView);
        make.size.mas_equalTo(CGSizeMake(200, 40));
    }];
    
    [progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(coverView);
        make.top.equalTo(waitImage.mas_bottom).offset(20);
        make.size.mas_equalTo(CGSizeMake(80, 80));
        
    }];
    
    return coverView;

}


-(void)pictureViewTepGestureAciton{
    ZCLog(@"放大图片");
    
    ZCShowBigPictureViewController *bigPictureVC = [[ZCShowBigPictureViewController alloc]init];
    bigPictureVC.model = self.model;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:bigPictureVC animated:YES completion:nil];
}


-(void)videoPlayBtnAction:(UIButton*)sender{
    ZCLog(@"播放视频");
    
}

-(void)voicePlayBtnAction:(UIButton*)sender{
    ZCLog(@"播放声音");
}

@end
